﻿/// <reference path="application.js" />

function Scene(name) {

    // Name of the scene: used to recover the folder in which the scene is
    this.name = name;
    
    // Invoked when the scene is ready
    this.onload = null;
    this.sceneImageLoaded = false;

    // sceneImage elements
    this.sceneImageSource = null;
    this.sceneImage = null;
    this.sceneCanvas = null;
    this.sceneCanvas2dContext = null;
    this.sceneImageData = null;

    this.sceneImageDataIsObsolete = true;
    
    if (typeof Scene.initialized == "undefined") {

        /// Initialize the scene
        Scene.prototype.Initialize = function () {
            this.sceneImageSource = app.context.rootUrl + "scenes/" + name + "/scene.png";
            this.sceneImage = new Image();
            this.sceneImage.onload = function () { this.sceneImageLoaded = true; this.TryFinalizeInitialization(); }.bind(this);
            this.sceneImage.src = this.sceneImageSource;
        };

        /// Check whether the scene is ready to use
        Scene.prototype.TryFinalizeInitialization = function () {

            // Check whether all loaded flags are set to true
            if (this.sceneImageLoaded) {

                // Finish initializing the scene
                this.sceneCanvas = document.createElement('canvas');
                this.sceneCanvas2dContext = this.sceneCanvas.getContext('2d');
                this.sceneCanvas.width = this.sceneImage.naturalWidth;
                this.sceneCanvas.height = this.sceneImage.naturalHeight;
                this.sceneCanvas2dContext.drawImage(this.sceneImage, 0, 0);
                this.RefreshSceneImageData();

                // Fire onload event
                this.onload();
            }
        };

        /// Draw method
        Scene.prototype.Draw = function () {
            app.context.container2d.drawImage(this.sceneCanvas,
                Math.round(app.UI.ToScreenX(0)),
                 Math.round(app.UI.ToScreenY(this.sceneCanvas.height)));
        };

        /// Think method
        Scene.prototype.Think = function () {
            if (this.sceneImageDataIsObsolete) {
                this.RefreshSceneImageData();
            }
        };

        /// Determines whether x y corrdinate in world reference is in collision with the map
        Scene.prototype.isInCollision = function (x, y) {
            return this.sceneImageData.data[(x + (this.sceneCanvas.height - y) * this.sceneImageData.width) * 4 + 3];
        };

        /// Destroy a part of the scene at x y coordinates in world reference
        Scene.prototype.Destroy = function (x, y, radius) {
            this.sceneCanvas2dContext.globalCompositeOperation = "destination-out";
            this.sceneCanvas2dContext.beginPath();
            this.sceneCanvas2dContext.arc(x, this.sceneCanvas.height - y, radius, 0, Math.PI * 2, true);
            this.sceneCanvas2dContext.closePath();
            this.sceneCanvas2dContext.fill();
            this.sceneCanvas2dContext.globalCompositeOperation = "source-over";

            this.sceneImageDataIsObsolete = true;
        };

        /// Refreshes scene image data
        Scene.prototype.RefreshSceneImageData = function() {
            this.sceneImageData = this.sceneCanvas2dContext.getImageData(0, 0, this.sceneCanvas.width, this.sceneCanvas.height);
            this.sceneImageDataIsObsolete = false;
        };

        Scene.initialized = true;
    }
}